import SimpleITK as sitk
import os

#directory=r'C:\Users\danch\Desktop\bad segment\section'
src_dir= '-MICCAI_BraTS2020_ValidationData0'
tgt_dir= '../brats2023/MICCAI_BraTS2020_ValidationData'
reader=sitk.ImageFileReader()
reader.SetImageIO("NiftiImageIO")
writer=sitk.ImageFileWriter()
for folder in os.listdir(src_dir):
    srcFolder_path = os.path.join(src_dir, folder)
    tgtFolder_path=os.path.join(tgt_dir,folder)

    if os.path.isdir(srcFolder_path):
        #create tgt folder
        if not os.path.isdir(tgtFolder_path):
            os.makedirs(tgtFolder_path)

        # print(folder_path)
        for file in os.listdir(srcFolder_path):
            file_path = os.path.join(srcFolder_path, file)
            if os.path.isfile(file_path):
                print(file_path)
                reader.SetFileName(file_path)
                image=reader.Execute()
                # print(image.GetOrigin())
                # print(file_path[:-3])
                writer.SetFileName(os.path.join(tgtFolder_path,file)+'.gz')
                writer.Execute(image)




